package com.kecheng.action;

import com.kecheng.dao.CommonDao;
import com.kecheng.dto.HotBanjiDto;
import com.kecheng.dto.WebCommonsResponse;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.Banji;
import com.kecheng.pojo.KeChengClassify;
import com.kecheng.pojo.Teacher;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台课程模块（比如：王老师+少儿英语+周一上午（9~10）），目前对号小班========此模块实际上改为了班级
 *
 * @author
 */

@ParentPackage("json-default")
@Action(value = "lesson", interceptorRefs = {@InterceptorRef("json")}, results = {
        @Result(name = "lessonList", location = "/admin/lesson/lessonList.jsp"),
        @Result(name = "toDetail", location = "/admin/lesson/detail.jsp"),
        @Result(name = "redirectList", type = "redirect", location = "lesson!lessonList.action"),
        @Result(name = "webResponse", type = "json", params = {"root", "webCommonsResponse"})
})
@AllowedMethods(value = {("regex:.*")})
public class LessonAction extends BaseAction {

    @Autowired
    CommonDao dao;

    private Banji lesson;
    private List<Integer> id;
    private PageBean<Banji> pageBean;
    private PageBean<HotBanjiDto> hotPageBean;
    private PageBean<Teacher>bookFilterTeacher;
    private List<Teacher> teacherList;
    private List<KeChengClassify> classifyList;

    /**
     * 用户列表
     *
     * @return
     */
    public String lessonList() {
        pageBean = (PageBean<Banji>) dao.findPageBean(Banji.class, pageBean);
        return "lessonList";
    }
    /**
     * 主页热门班级
     *
     * @return
     */
    public String hotLessonList() {
        if (com.kecheng.com.utils.CommonUtil.isEmpty(lesson)) {
            lesson = new Banji();
        }
        lesson.setIsHot(1);
        wxLessonList();
        return "webResponse";
    }

    private void wxLessonList() {
        pageBean = (PageBean<Banji>) dao.findPageBeanByObj(lesson, pageBean);
        hotPageBean = new PageBean<HotBanjiDto>();
        BeanUtils.copyProperties(pageBean, hotPageBean);
        //循环查询老师资料
        List<Banji> banjiList = pageBean.getData();
        if (banjiList != null) {
            List<HotBanjiDto> hotBanjiList = new ArrayList<>();
            HotBanjiDto hotBanji = null;
            Teacher teacher = new Teacher();
            for (Banji bj : banjiList) {
                hotBanji = new HotBanjiDto();
                hotBanji.setBanji(bj);

                teacher = new Teacher();
                teacher.setId(bj.getTeacherId());
                teacher = (Teacher) dao.findOne(teacher);
                hotBanji.setTeacher(teacher);
                hotBanjiList.add(hotBanji);
            }
            hotPageBean.setData(hotBanjiList);
        }
        webCommonsResponse = WebCommonsResponse.ok(hotPageBean);
    }

    /**
     * 个人详情页
     *
     * @return
     */
    public String toDetail() {
        teacherList = (List<Teacher>) dao.find(Teacher.class);
        if (lesson != null) {
            lesson = (Banji) dao.findOne(lesson);
        }
        KeChengClassify classify = new KeChengClassify();
        classify.setParentId(0);
        classifyList = (List<KeChengClassify>) dao.find(classify);

        return "toDetail";
    }

    /**
     * 更新或者添加
     */
    @Transactional(readOnly = false)
    public String save() {
        if (lesson.getId() != null) {
            Banji lesson1 = new Banji();
            lesson1.setId(lesson.getId());
            dao.delete(lesson);
        }

        lesson.setOneClassifyId(Integer.valueOf(lesson.getOneClassifyName().split("-")[0]));
        lesson.setTeacherId(Integer.valueOf(lesson.getTeacherName().split("-")[0]));
        lesson.setTwoClassifyId(Integer.valueOf(lesson.getTwoClassifyName().split("-")[0]));

        lesson.setOneClassifyName(lesson.getOneClassifyName().split("-")[1]);
        lesson.setTeacherName(lesson.getTeacherName().split("-")[1]);
        lesson.setTwoClassifyName(lesson.getTwoClassifyName().split("-")[1]);
        dao.add(lesson);

        return "redirectList";
    }

    /**
     * 删除
     *
     * @return
     */
    @Transactional(readOnly = false)
    public String delete() {
        if (lesson != null) dao.delete(lesson);
        else if (id != null) {
            for (Integer i : id) {
                lesson = new Banji();
                lesson.setId(i);
                dao.delete(lesson);
            }
        }
        return "redirectList";
    }


    public Banji getLesson() {
        return lesson;
    }

    public void setLesson(Banji lesson) {
        this.lesson = lesson;
    }

    public List<Integer> getId() {
        return id;
    }

    public void setId(List<Integer> id) {
        this.id = id;
    }

    public PageBean<Banji> getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean<Banji> pageBean) {
        this.pageBean = pageBean;
    }

    public List<Teacher> getTeacherList() {
        return teacherList;
    }

    public void setTeacherList(List<Teacher> teacherList) {
        this.teacherList = teacherList;
    }

    public List<KeChengClassify> getClassifyList() {
        return classifyList;
    }

    public void setClassifyList(List<KeChengClassify> classifyList) {
        this.classifyList = classifyList;
    }

    public PageBean<HotBanjiDto> getHotPageBean() {
        return hotPageBean;
    }

    public void setHotPageBean(PageBean<HotBanjiDto> hotPageBean) {
        this.hotPageBean = hotPageBean;
    }

    public PageBean<Teacher> getBookFilterTeacher() {
        return bookFilterTeacher;
    }

    public void setBookFilterTeacher(PageBean<Teacher> bookFilterTeacher) {
        this.bookFilterTeacher = bookFilterTeacher;
    }
}
